import 'dart:convert';

class Exercise {
  final String? exerciseId;
  final String name;
  final String description;
  final String gymId;
  final String fromTime;
  final String toTime;
  final String targetGroup;
  final String? active;
  final DateTime? createAt;
  final DateTime? updateAt;
  final String? createdBy;
  final String? isDeleted;

  Exercise({
    this.exerciseId,
    required this.name,
    required this.description,
    required this.gymId,
    required this.fromTime,
    required this.toTime,
    required this.targetGroup,
    this.active,
    this.createAt,
    this.updateAt,
    this.createdBy,
    this.isDeleted,
  });

  Exercise copyWith({
    String? exerciseId,
    String? name,
    String? description,
    String? gymId,
    String? fromTime,
    String? toTime,
    String? targetGroup,
    String? active,
    DateTime? createAt,
    DateTime? updateAt,
    String? createdBy,
    String? isDeleted,
  }) =>
      Exercise(
        exerciseId: exerciseId ?? this.exerciseId,
        name: name ?? this.name,
        description: description ?? this.description,
        gymId: gymId ?? this.gymId,
        fromTime: fromTime ?? this.fromTime,
        toTime: toTime ?? this.toTime,
        targetGroup: targetGroup ?? this.targetGroup,
        active: active ?? this.active,
        createAt: createAt ?? this.createAt,
        updateAt: updateAt ?? this.updateAt,
        createdBy: createdBy ?? this.createdBy,
        isDeleted: isDeleted ?? this.isDeleted,
      );

  factory Exercise.fromRawJson(String str) => Exercise.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory Exercise.fromJson(Map<String, dynamic> json) => Exercise(
    exerciseId: json["exercise_id"]?.toString(),
    name: json["name"],
    description: json["description"],
    gymId: json["gym_id"].toString(),
    fromTime: json["from_time"].toString().substring(0,5),
    toTime: json["to_time"].toString().substring(0,5),
    targetGroup: json["target_group"],
    active: json["active"]?.toString(),
    createAt: DateTime.parse(json["create_at"]),
    updateAt: DateTime.parse(json["update_at"]),
    createdBy: json["created_by"]?.toString(),
    isDeleted: json["is_deleted"]?.toString(),
  );

  Map<String, dynamic> toJson() => {
    "exercise_id": exerciseId,
    "name": name,
    "description": description,
    "gym_id": gymId,
    "from_time": fromTime,
    "to_time": toTime,
    "target_group": targetGroup,
  };
}
